package com.colorful.insuranceproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.bean.PayMethod;
import com.lovely3x.common.adapter.BaseViewHolder;
import com.lovely3x.common.adapter.ListAdapter;

import java.util.List;

import butterknife.Bind;

/**
 * 支付方式适配器
 * Created by lovely3x on 16/8/13.
 */
public class PayMethodListAdapter extends ListAdapter<PayMethod> {

    public PayMethodListAdapter(List<PayMethod> datas, Context context) {
        super(datas, context);
        setCheckMode(AbsListView.CHOICE_MODE_SINGLE);
    }

    @NonNull
    @Override
    public BaseViewHolder createViewHolder(int position, ViewGroup parent) {
        return new ViewHolder(getLayoutInflater().inflate(R.layout.view_pay_method_item, parent, false));
    }

    @Override
    public void handleData(final int position, @NonNull BaseViewHolder holder) {
        ViewHolder viewHolder = (ViewHolder) holder;

        final PayMethod item = getItem(position);

        viewHolder.tvPayMethodName.setText(item.payMethodName);
        viewHolder.tvPayMethodName.setCompoundDrawablesWithIntrinsicBounds(item.iconSourceId, 0, 0, 0);
        viewHolder.rbIsChecked.setChecked(isChecked(item.typeId));
        viewHolder.rbIsChecked.setEnabled(item.isAvailable);

        if (!item.payMethodName.equals(mContext.getString(R.string.appname))) {
            viewHolder.tvMoney.setText(mContext.getString(R.string.account_limited, "0"));
            viewHolder.tvMoney.setVisibility(View.GONE);
        } else {
            if (!isChecked(item.typeId)) {
                viewHolder.tvPayMethodName.setTextColor(ContextCompat.getColor(mContext, R.color.auxiliary_text_bg));
                viewHolder.tvMoney.setTextColor(ContextCompat.getColor(mContext, R.color.auxiliary_text_bg));
            }
            viewHolder.tvMoney.setText(mContext.getString(R.string.account_limited, String.valueOf(item.money)));
            viewHolder.tvMoney.setVisibility(View.VISIBLE);
        }

        viewHolder.mRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickedListener != null)
                    mOnItemClickedListener.onClicked(position, item);
            }
        });

    }

    public static class ViewHolder extends BaseViewHolder {

        /**
         * 支付方式名
         */
        @Bind(R.id.tv_view_pay_method_item_method_name)
        TextView tvPayMethodName;

        /**
         * 是否选中
         */
        @Bind(R.id.rb_view_pay_method_item_is_checked)
        RadioButton rbIsChecked;

        /**
         * 可康安余额
         */
        @Bind(R.id.tv_money)
        TextView tvMoney;

        public ViewHolder(View rootView) {
            super(rootView);
        }
    }
}
